<template>
  <div class="login-container">
    <div class="login-box">
      <h2 class="login-title">侦查攻击训练武器载荷库</h2>
      <el-form :model="form" :rules="rules" ref="loginForm" label-width="100px">
        <el-form-item label="用户名" prop="name">
          <el-input
            v-model="form.name"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" class="login-button">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import $axios from "@/unil";
import { addAsyncRoutes } from "@/router";

export default {
  data() {
    return {
      form: {
        name: "admin",
        password: "123456",
      },
      rules: {
        name: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    async onSubmit() {
      const valid = await new Promise((resolve) => {
        this.$refs.loginForm.validate((valid) => {
          resolve(valid);
        });
      });
      if (valid) {
        try {
          const response = await $axios.post("/user/login", this.form);
          console.log("登录响应:", response);

          if (response.data.code === 0) {
            const { token, username, permissions } = response.data;

            localStorage.setItem("token", token);
            localStorage.setItem("username", username);
            localStorage.setItem("permissions", JSON.stringify(permissions));

            this.$store.dispatch("permissions/setUsername", username);
            this.$store.dispatch("permissions/setPermissions", permissions);

            addAsyncRoutes(permissions);

            this.$message.success("登录成功");
            this.$router.push("/layout");
          } else {
            this.$message.error("登录失败，请检查用户名和密码");
          }
        } catch (error) {
          console.error("登录请求失败:", error);
          this.$message.error("登录请求失败，请稍后再试");
        }
      }
    },
  },
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #1e3c72, #2a5298, #1c2a5a);
  background-size: 400% 400%;
  animation: gradientAnimation 15s ease infinite;
  position: relative;
  overflow: hidden;
}

.login-container::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('https://api.ixiaowai.cn/gqapi/gqapi.php') no-repeat center center;
  background-size: cover;
  opacity: 0.2;
  z-index: 0;
}

.login-box {
  width: 450px;
  padding: 40px;
  border-radius: 20px;
  background-color: rgba(46, 46, 46, 0.8);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  text-align: center;
  animation: fadeIn 1s ease-in-out;
  border: 1px solid rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  position: relative;
  z-index: 1;
}

.login-title {
  font-size: 32px;
  font-weight: bold;
  margin-bottom: 30px;
  color: #fff;
  text-shadow: 0 0 10px rgba(0, 140, 255, 0.5);
  letter-spacing: 2px;
  position: relative;
}

.login-title::after {
  content: "";
  position: absolute;
  width: 100px;
  height: 3px;
  background: linear-gradient(90deg, transparent, #409eff, transparent);
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
}

.el-form-item {
  margin-bottom: 25px;
}

.login-button {
  width: 100%;
  height: 50px;
  font-size: 18px;
  font-weight: bold;
  border-radius: 25px;
  background: linear-gradient(90deg, #3a8ee6, #409eff, #3a8ee6) !important;
  background-size: 200% auto !important;
  color: #fff;
  letter-spacing: 2px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
  transition: all 0.5s ease;
  border: none !important;
  margin-top: 10px;
}

.login-button:hover {
  background-position: right center !important;
  transform: translateY(-3px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
}

.login-button:active {
  transform: translateY(0);
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes gradientAnimation {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

::v-deep(.el-form) {
  background-color: transparent;
}

::v-deep(.el-form .el-form-item .el-form-item__label) {
  color: #e0e0e0 !important;
  font-size: 16px;
  font-weight: 500;
}

::v-deep(.el-form-item__content .el-input .el-input__inner) {
  background: rgba(255, 255, 255, 0.05) !important;
  color: #fff !important;
  border: 1px solid rgba(255, 255, 255, 0.1) !important;
  border-radius: 10px;
  height: 45px;
  padding-left: 35px; /* 增加左侧内边距，为图标留出空间 */
  transition: all 0.3s ease;
}

::v-deep(.el-form-item__content .el-input .el-input__inner:focus) {
  border-color: #409eff !important;
  box-shadow: 0 0 10px rgba(64, 158, 255, 0.3);
}

::v-deep(.el-input__prefix) {
  color: #999;
  font-size: 18px;
  left: 8px; /* 调整图标的左侧位置 */
}

::v-deep(.el-form-item.is-error .el-input__inner) {
  border-color: #f56c6c !important;
  box-shadow: 0 0 10px rgba(245, 108, 108, 0.2);
}
</style>
